package Dao;



import mapper.Usermapper;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import Entity.User;
public  class userdao implements Usermapper{
	private  static Usermapper usermapper;
	private  static SqlSession session;

	public userdao() throws IOException {
	 	String resource ="mybatis-config.xml";
		InputStream inputstream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlsessionfactory = new SqlSessionFactoryBuilder().build(inputstream);
		 session = sqlsessionfactory.openSession();
        usermapper = session.getMapper(Usermapper.class);
    }
	public void close(){
		this.session.close();
	}
	@Override
	public boolean CreatAccount(User user) {
	
		Boolean iuser = this.usermapper.CreatAccount(user);
		if(iuser == true){
			session.commit();
		}else{
			session.rollback();
		}

		return iuser;
	}

	@Override
	public void Change_Name(User user) {
		
		this.usermapper.Change_Name(user);
		
		session.commit();

		
		
	}

	@Override
	public void Change_Pwd(User user) {
	
		this.usermapper.Change_Pwd(user);
		
		session.commit();

		
		
	}

	@Override
	public User Validate(User user) {
	
		User us = this.usermapper.Validate(user);
		return us;
	}

	@Override
	public List<User> SearchUser(String searh) {
		List<User> selectList = this.usermapper.SearchUser(searh);
		return selectList;

	}

	@Override
	public User findUser(String uid) {
		
		User us = this.usermapper.findUser(uid);

		return us;
	}

	@Override
	public List<User> SearchTeacher() {
		
		List<User> selectList =this.usermapper.SearchTeacher();

		
		return selectList;
	}

	@Override
	public List<User> SearchStudent() {
		
		List<User> selectList = this.usermapper.SearchStudent();

		return selectList;
	}

	@Override
	public List<User> SearchTeacherBypage( int page) {
		Usermapper um = null;
		try {
			um = new userdao();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<User> u1 = um.SearchTeacher();
		//        浠庣鍑犳潯鏁版嵁寮�濮�
        int firstIndex = (page - 1) * 10;
        //	        鍒扮鍑犳潯鏁版嵁缁撴潫
        int lastIndex = page * 10;
        if(lastIndex<=u1.size())
        	return u1.subList(firstIndex, lastIndex);
        else if(firstIndex>=u1.size())
        	return null;
        else
        	return u1.subList(firstIndex, u1.size());
	}

	@Override
	public List<User> SearchStudentBypage( int page) {
		Usermapper um = null;
		try {
			um = new userdao();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<User> u1 = um.SearchStudent();
		//        浠庣鍑犳潯鏁版嵁寮�濮�
        int firstIndex = (page - 1) * 10;
        //	        鍒扮鍑犳潯鏁版嵁缁撴潫
        int lastIndex = page * 10;
        if(lastIndex<=u1.size())
        	return u1.subList(firstIndex, lastIndex);
        else if(firstIndex>=u1.size())
        	return null;
        else
        	return u1.subList(firstIndex, u1.size());
	}

	@Override
	public List<User> SearchUserBypage(String searh, int page) {
		Usermapper um = null;
		try {
			um = new userdao();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<User> u1 = um.SearchUser(searh);
		//        浠庣鍑犳潯鏁版嵁寮�濮�
        int firstIndex = (page - 1) * 10;
        //	        鍒扮鍑犳潯鏁版嵁缁撴潫
        int lastIndex = page * 10;
        if(lastIndex<=u1.size())
        	return u1.subList(firstIndex, lastIndex);
        else if(firstIndex>=u1.size())
        	return null;
        else
        	return u1.subList(firstIndex, u1.size());
	}

}
